<template>
  <div class="vue-admin-beautiful-wrapper" :class="classObj">
    <div
      v-if="'horizontal' === layout"
      class="layout-container-horizontal"
      :class="{
        fixed: header === 'fixed',
        'no-tabs-bar': tabsBar === 'false' || tabsBar === false,
      }"
    >
      <div :class="header === 'fixed' ? 'fixed-header' : ''">
        <vab-top-bar />
        <div
          v-if="tabsBar === 'true' || tabsBar === true"
          :class="{ 'tag-view-show': tabsBar }"
        >
          <div class="vab-main">
            <vab-tabs-bar />
          </div>
        </div>
      </div>
      <div class="vab-main main-padding">
        <!-- 收缩菜单 -->

        <vab-app-main />
      </div>
    </div>
    <div
      v-else
      class="layout-container-vertical"
      :class="{
        fixed: header === 'fixed',
        'no-tabs-bar': tabsBar === 'false' || tabsBar === false,
      }"
    >
      <div
        v-if="device === 'mobile' && collapse === false"
        class="mask"
        @click="handleFoldSideBar"
      />
      <div
        class="scalmenu"
        @click="handleCollapse"
        :style="{ left: !collapse ? '210px' : '64px' }"
      >
        <i
          :class="collapse ? 'el-icon-arrow-right' : 'el-icon-arrow-left'"
          :title="collapse ? '展开' : '收起'"
          class="fold-unfold"
        ></i>
      </div>
      <vab-side-bar />
      <div class="vab-main" :class="collapse ? 'is-collapse-main' : ''">
        <div :class="header === 'fixed' ? 'fixed-header' : ''">
          <vab-nav-bar />
          <!-- <vab-tabs-bar v-if="tabsBar === 'true' || tabsBar === true" /> -->
        </div>
        <vab-breadcrumb class="hidden-xs-only" style="margin-left: 30px" />
        <vab-app-main
          style="margin-left: 25px; box-sizing: border-box; border-radius: 10px"
        />
      </div>
    </div>
    <el-backtop :right="20" :bottom="40" />
    <el-tooltip class="kefuTip" placement="left" effect="light">
      <div slot="content">
        <span class="tipsText" v-if="steward.nickname || steward.tel || steward.phone">
          客服：{{ steward.nickname }}
          <br />
          电话：{{ steward.tel }}
          <br />
          座机：{{ steward.phone }}
        </span>
        <span v-else>暂无信息</span>
      </div>
      <img src="../assets/kefu.png" alt="" />
    </el-tooltip>
  </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'
import { tokenName } from '@/config'
import { getSessionStorage } from '@/utils/accessToken'
export default {
  name: 'Layout',
  data() {
    return { oldLayout: '', steward: null }
  },
  computed: {
    ...mapGetters({
      layout: 'settings/layout',
      tabsBar: 'settings/tabsBar',
      collapse: 'settings/collapse',
      header: 'settings/header',
      device: 'settings/device',
    }),
    classObj() {
      return {
        mobile: this.device === 'mobile',
      }
    },
  },
  beforeMount() {
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
  },
  mounted() {
    this.steward = getSessionStorage('userinfo').steward
    this.oldLayout = this.layout
    const userAgent = navigator.userAgent
    if (userAgent.includes('Juejin')) {
      this.$baseAlert(
        '不支持在掘金内置浏览器演示，请手动复制以下地址到浏览器中查看http://mpfhrd48.sanxing.uz7.cn/vue-admin-beautiful'
      )
    }
    const isMobile = this.handleIsMobile()
    if (isMobile) {
      if (isMobile) {
        //横向布局时如果是手机端访问那么改成纵向版
        this.$store.dispatch('settings/changeLayout', 'vertical')
      } else {
        this.$store.dispatch('settings/changeLayout', this.oldLayout)
      }
      this.$store.dispatch('settings/toggleDevice', 'mobile')
      setTimeout(() => {
        this.$store.dispatch('settings/foldSideBar')
      }, 2000)
    } else {
      this.$store.dispatch('settings/openSideBar')
    }
    this.$nextTick(() => {
      window.addEventListener(
        'storage',
        (e) => {
          if (e.key === tokenName || e.key === null) window.location.reload()
          if (e.key === tokenName && e.value === null) window.location.reload()
        },
        false
      )
    })
  },
  methods: {
    ...mapActions({
      changeCollapse: 'settings/changeCollapse',
      handleFoldSideBar: 'settings/foldSideBar',
    }),
    handleIsMobile() {
      return document.body.getBoundingClientRect().width - 1 < 992
    },
    handleCollapse() {
      //  console.log(this.collapse)
      this.changeCollapse()
    },
    handleResize() {
      if (!document.hidden) {
        const isMobile = this.handleIsMobile()
        if (isMobile) {
          //横向布局时如果是手机端访问那么改成纵向版
          this.$store.dispatch('settings/changeLayout', 'vertical')
        } else {
          this.$store.dispatch('settings/changeLayout', this.oldLayout)
        }

        this.$store.dispatch(
          'settings/toggleDevice',
          isMobile ? 'mobile' : 'desktop'
        )
      }
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep {
  .kefuTip {
    position: fixed;
    bottom: 100px;
    right: 20px;
    width: 40px;
    height: 40px;
    z-index: 2021;
  }
    .tipsText{
      line-height: 20px;
    }
}
.scalmenu {
  position: fixed;
  width: 16px;
  height: 81px;
  background-color: #fff;
  top: 50%;
  left: 64px;
  z-index: 1300;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transform: translate(0, -50%);
  transition: color 0.3s ease;
  border: 1px solid #e4e7f0;
  border-left-width: 0;
  border-top-right-radius: 16px;
  border-bottom-right-radius: 16px;
}

@mixin fix-header {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: $base-z-index - 2;
  width: 100%;
  overflow: hidden;
}

.vue-admin-beautiful-wrapper {
  position: relative;
  width: 100%;
  height: 100%;

  .layout-container-horizontal {
    position: relative;

    &.fixed {
      padding-top: calc(#{$base-top-bar-height} + #{$base-tabs-bar-height});
    }

    &.fixed.no-tabs-bar {
      padding-top: $base-top-bar-height;
    }

    ::v-deep {
      .vab-main {
        width: 88%;
        margin: auto;
      }

      .fixed-header {
        @include fix-header;
      }

      .tag-view-show {
        background: $base-color-white;
        box-shadow: $base-box-shadow;
      }

      .nav-bar-container {
        .fold-unfold {
          display: none;
        }
      }

      .main-padding {
        .app-main-container {
          margin-top: $base-padding;
          margin-bottom: $base-padding;
          background: $base-color-white;
        }
      }
    }
  }

  .layout-container-vertical {
    position: relative;

    .mask {
      position: fixed;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: $base-z-index - 1;
      width: 100%;
      height: 100vh;
      overflow: hidden;
      background: #000;
      opacity: 0.5;
    }

    &.fixed {
      padding-top: calc(14px + #{$base-tabs-bar-height});
    }

    &.fixed.no-tabs-bar {
      padding-top: $base-nav-bar-height;
    }

    .vab-main {
      position: relative;
      min-height: 100%;
      margin-left: $base-left-menu-width;
      background: #f6f8f9;
      transition: $base-transition;

      ::v-deep {
        .fixed-header {
          @include fix-header;

          // left: $base-left-menu-width;
          width: 100%;
          box-shadow: 0 8px 28px 0 rgba(192, 206, 226, 0.13);
          transition: $base-transition;
          z-index: 999;
        }

        .nav-bar-container {
          position: relative;
          box-sizing: border-box;
        }

        .tabs-bar-container {
          box-sizing: border-box;
        }

        .app-main-container {
          width: calc(100% - #{$base-padding} - #{$base-padding});
          margin: 10px auto 10px;
          // background: $base-color-white;
          border-radius: $base-border-radius;
        }
      }

      &.is-collapse-main {
        margin-left: $base-left-menu-width-min;

        ::v-deep {
          .fixed-header {
            // left: $base-left-menu-width-min;
            width: calc(100%);
          }
        }
      }
    }
  }

  /* 手机端开始 */
  &.mobile {
    ::v-deep {
      .el-pager,
      .el-pagination__jump {
        display: none;
      }

      .layout-container-vertical {
        .el-scrollbar.side-bar-container.is-collapse {
          width: 0;
        }

        .vab-main {
          width: 100%;
          margin-left: 0;
        }
      }

      .vab-main {
        .fixed-header {
          left: 0 !important;
          width: 100% !important;
        }
      }
    }
  }

  /* 手机端结束 */
}
</style>
